﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Revit_Addin1 // Namesspace used in each class inorder to have global variables
{
    public partial class MaterialProperties : Form // Splits class between files
    {
        public MaterialProperties() // Creates public variable to be utilized in other classes 
        {
            InitializeComponent();
        }
        //Form values when loaded
        private void BIMformation_Load(object sender, EventArgs e)
        {
            //Clear all previous values in the list
            StoredVariables.Types = StoredVariables.FloorTypes;
            StoredVariables.Roughness = StoredVariables.FloorRoughness;
            StoredVariables.ThermalResistance = StoredVariables.FloorThermalResistance;
            StoredVariables.ThermalAbsorptance = StoredVariables.FloorThermalAbsorptance;
            StoredVariables.SolarAbsorptance = StoredVariables.FloorSolarAbsorptance;
            StoredVariables.VisibleAbsorptance = StoredVariables.FloorVisibleAbsorptance;

            //Set intial values of the form to prevent list box errors
            this.MaterialCombo.SelectedIndex = 0;
            this.MaterialList.Items.Clear();
            this.MaterialList.Items.AddRange(StoredVariables.Types.ToArray());
            this.MaterialList.SelectedIndex = 0;
            this.RoughnessCombo.SelectedIndex = this.RoughnessCombo.Items.IndexOf("MediumRough");
        }

        //Compile on completion of form
        private void OKbutton_Click(object sender, EventArgs e)
        {
            MaterialProperties form = (sender as System.Windows.Forms.Control).Parent as MaterialProperties;
            form.DialogResult = DialogResult.OK;

            IDFcompiler compiler = new IDFcompiler();

            compiler.Compile();

            form.Close();
        }

        //Close the form on cancel
        private void Cancelbutton_Click(object sender, EventArgs e)
        {
            MaterialProperties form = (sender as System.Windows.Forms.Control).Parent as MaterialProperties;
            form.DialogResult = DialogResult.Cancel;
            
            form.Close();
        }

        //Set values of name, conductivity, and thermal resistance when material is changed
        private void MaterialList_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.NameBox.Text = StoredVariables.Types[this.MaterialList.SelectedIndex];
            this.ConductivityBox.Text = StoredVariables.ThermalResistance[this.MaterialList.SelectedIndex].ToString();
            this.ThermalNum.Value = Convert.ToDecimal(StoredVariables.ThermalAbsorptance[this.MaterialList.SelectedIndex]);
        }

        //Rewrite roughness value based on index currently chosen
        private void RoughnessCombo_SelectedIndexChanged(object sender, EventArgs e)
        {
            //First remove old value, then apply new value
            StoredVariables.Roughness.RemoveAt(this.MaterialList.SelectedIndex);
            StoredVariables.Roughness.Insert(this.MaterialList.SelectedIndex,this.RoughnessCombo.SelectedItem.ToString());

            if (this.MaterialCombo.SelectedIndex == 0)
            {
                StoredVariables.FloorRoughness = StoredVariables.Roughness;
            }
            else if (this.MaterialCombo.SelectedIndex == 1)
            {
                StoredVariables.WallRoughness = StoredVariables.Roughness;
            }
            else if (this.MaterialCombo.SelectedIndex == 2)
            {
                StoredVariables.RoofRoughness = StoredVariables.Roughness;
            }

        }

        //Rewrite thermal absorptance value based on index currently chosen
        private void ThermalNum_ValueChanged(object sender, EventArgs e)
        {
            //First remove old value, then apply new value
            StoredVariables.ThermalAbsorptance.RemoveAt(this.MaterialList.SelectedIndex);
            StoredVariables.ThermalAbsorptance.Insert(this.MaterialList.SelectedIndex, Convert.ToDouble(this.ThermalNum.Value));

            if (this.MaterialCombo.SelectedIndex == 0)
            {
                StoredVariables.FloorThermalAbsorptance = StoredVariables.ThermalAbsorptance;
            }
            else if (this.MaterialCombo.SelectedIndex == 1)
            {
                StoredVariables.WallThermalAbsorptance = StoredVariables.ThermalAbsorptance;
            }
            else if (this.MaterialCombo.SelectedIndex == 2)
            {
                StoredVariables.RoofThermalAbsorptance = StoredVariables.ThermalAbsorptance;
            }
        }

        //Rewrite solar absorptance value based on index currently chosen
        private void SolarNum_ValueChanged(object sender, EventArgs e)
        {
            //First remove old value, then apply new value
            StoredVariables.SolarAbsorptance.RemoveAt(this.MaterialList.SelectedIndex);
            StoredVariables.SolarAbsorptance.Insert(this.MaterialList.SelectedIndex, Convert.ToDouble(this.SolarNum.Value));

            if (this.MaterialCombo.SelectedIndex == 0)
            {
                StoredVariables.FloorSolarAbsorptance = StoredVariables.SolarAbsorptance;
            }
            else if (this.MaterialCombo.SelectedIndex == 1)
            {
                StoredVariables.WallSolarAbsorptance = StoredVariables.SolarAbsorptance;
            }
            else if (this.MaterialCombo.SelectedIndex == 2)
            {
                StoredVariables.RoofSolarAbsorptance = StoredVariables.SolarAbsorptance;
            }
        }

        //Rewrite visible absorptance value based on index currently chosen
        private void VisibleNum_ValueChanged(object sender, EventArgs e)
        {
            //First remove old value, then apply new value
            StoredVariables.VisibleAbsorptance.RemoveAt(this.MaterialList.SelectedIndex);
            StoredVariables.VisibleAbsorptance.Insert(this.MaterialList.SelectedIndex, Convert.ToDouble(this.VisibleNum.Value));

            if (this.MaterialCombo.SelectedIndex == 0)
            {
                StoredVariables.FloorVisibleAbsorptance = StoredVariables.VisibleAbsorptance;
            }
            else if (this.MaterialCombo.SelectedIndex == 1)
            {
                StoredVariables.WallVisibleAbsorptance = StoredVariables.VisibleAbsorptance;
            }
            else if (this.MaterialCombo.SelectedIndex == 2)
            {
                StoredVariables.RoofVisibleAbsorptance = StoredVariables.VisibleAbsorptance;
            }
        }

        //Alter list box values based on material type chosen 
        private void MaterialCombo_TextChanged(object sender, EventArgs e)
        {
             if (this.MaterialCombo.SelectedIndex == 0)
            {
                StoredVariables.Types = StoredVariables.FloorTypes;
                StoredVariables.Roughness = StoredVariables.FloorRoughness;
                StoredVariables.ThermalResistance = StoredVariables.FloorThermalResistance;
                StoredVariables.ThermalAbsorptance = StoredVariables.FloorThermalAbsorptance;
                StoredVariables.SolarAbsorptance = StoredVariables.FloorSolarAbsorptance;
                StoredVariables.VisibleAbsorptance = StoredVariables.FloorVisibleAbsorptance;
            }
            else if (this.MaterialCombo.SelectedIndex == 1)
            {
                StoredVariables.Types = StoredVariables.WallTypes;
                StoredVariables.Roughness = StoredVariables.WallRoughness;
                StoredVariables.ThermalResistance = StoredVariables.WallThermalResistance;
                StoredVariables.ThermalAbsorptance = StoredVariables.WallThermalAbsorptance;
                StoredVariables.SolarAbsorptance = StoredVariables.WallSolarAbsorptance;
                StoredVariables.VisibleAbsorptance = StoredVariables.WallVisibleAbsorptance;
            }
            else if (this.MaterialCombo.SelectedIndex == 2)
            {
                StoredVariables.Types = StoredVariables.RoofTypes;
                StoredVariables.Roughness = StoredVariables.RoofRoughness;
                StoredVariables.ThermalResistance = StoredVariables.RoofThermalResistance;
                StoredVariables.ThermalAbsorptance = StoredVariables.RoofThermalAbsorptance;
                StoredVariables.SolarAbsorptance = StoredVariables.RoofSolarAbsorptance;
                StoredVariables.VisibleAbsorptance = StoredVariables.RoofVisibleAbsorptance;
            }

            //Clear List Items then add new array to list box and select the first element
            this.MaterialList.Items.Clear();
            this.MaterialList.Items.AddRange(StoredVariables.Types.ToArray());
            this.MaterialList.SelectedIndex = 0;
        }
    }
}
